package cn.zn.modules.pz.cur.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.modules.pz.cur.dao.TpSpecialMapper;
import cn.zn.modules.pz.cur.entity.TpSpecial;
import cn.zn.modules.pz.cur.entity.TpSpecialTo;
import cn.zn.modules.pz.cur.service.TpSpecialService;
import cn.zn.modules.pz.cur.service.TpSpecialToService;
import cn.zn.modules.zzsys.inter.TeacherInter;
import cn.zn.modules.zzsys.inter.vo.TeacherInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TpSpecialServiceImpl extends ServiceImpl<TpSpecialMapper, TpSpecial> implements TpSpecialService {

    @Autowired
    private TpSpecialToService tpSpecialToService;
    @Autowired
    private TeacherInter teacherInter;

    @Override
    public TpSpecial findByMainId(String mainId) {
        if (teacherInter == null) {
            throw new RRException("未加载教师模块");
        }
        TpSpecial tpSpecial = super.getOne(new QueryWrapper<TpSpecial>().eq("TP_MAIN_ID_", mainId));
        if (tpSpecial != null) {
            List<TpSpecialTo> tpSpecialTos = tpSpecialToService.list(new QueryWrapper<TpSpecialTo>()
                    .eq("TP_SPECIAL_ID_", tpSpecial.getId())
                    .orderByAsc("CREATE_TIME_")
            );
            if (tpSpecialTos.size() > 0) {
                Map<String, TeacherInfoVO> teacherInfoVOMap = teacherInter.findByWorkNos(tpSpecialTos.stream().map(o -> o.getUserCode()).collect(Collectors.toList()));
                List<String> userCodes = CollUtil.newArrayList();
                List<String> userNames = CollUtil.newArrayList();
                teacherInfoVOMap.forEach((k, v) -> {
                    userCodes.add(k);
                    userNames.add(v.getName());
                });
                tpSpecial.setUserCodes(ArrayUtil.toArray(userCodes, String.class));
                tpSpecial.setUserNames(ArrayUtil.toArray(userNames, String.class));
            }
        }
        return tpSpecial;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpecial(TpSpecial tpSpecial) {
        super.save(tpSpecial);
        this.saveSpecialTo(tpSpecial);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpecial(TpSpecial tpSpecial) {
        super.updateById(tpSpecial);
        tpSpecialToService.remove(new QueryWrapper<TpSpecialTo>().eq("TP_SPECIAL_ID_", tpSpecial.getId()));
        this.saveSpecialTo(tpSpecial);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSpecial(String mainId) {
        TpSpecial tpSpecial = super.getOne(new QueryWrapper<TpSpecial>().eq("TP_MAIN_ID_", mainId));
        if (tpSpecial != null) {
            tpSpecialToService.remove(new QueryWrapper<TpSpecialTo>().eq("TP_SPECIAL_ID_", tpSpecial.getId()));
            super.removeById(tpSpecial.getId());
        }
    }

    private void saveSpecialTo(TpSpecial tpSpecial) {
        if (tpSpecial.getUserCodes() != null && tpSpecial.getUserCodes().length > 0) {
            List<TpSpecialTo> toList = CollUtil.newArrayList();
            for (int i = 0; i < tpSpecial.getUserCodes().length; i++) {
                toList.add(TpSpecialTo.builder()
                        .tpSpecialId(tpSpecial.getId())
                        .userCode(tpSpecial.getUserCodes()[i])
                        .build());
            }
            tpSpecialToService.saveBatch(toList);
        }
    }
}
